/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package be.cultuurschuur.services;

import be.cultuurschuur.Controllers.*;
import be.cultuurschuur.entity.*;
import be.cultuurschuur.interfaces.*;
import java.io.*;
import java.util.*;
import java.util.logging.*;
import javax.faces.context.*;
import javax.persistence.*;



/**
 *
 * @author Jolien
 */
public class BLContinent implements ContinentInterface
{
    EntityManagerFactory emf = Persistence.createEntityManagerFactory("cultuurschuurPU");
    EntityManager em = emf.createEntityManager();
    EntityTransaction tx = em.getTransaction();
    
    private String continentNaam;

    public String getContinentNaam() {
        return continentNaam;
    }

    public void setContinentNaam(String continentNaam) {
        this.continentNaam = continentNaam;
    }

    @Override
    public Continent insertContinent()
    { 
        tx.begin();
        Continent continent = new Continent();
        continent.setContinentNaam(continentNaam);
        
        em.persist(continent);
        tx.commit();
        try {
            FacesContext.getCurrentInstance().getExternalContext().redirect("continent.xhtml");
        } catch (IOException ex) {
            Logger.getLogger(VervoerController.class.getName()).log(Level.SEVERE, null, ex);
        }
        return continent;
    }
    
    @Override
    public List<Continent> getContinenten()
    {
        return em.createNamedQuery("Continent.getAll").getResultList();
    }
    
    @Override
    public Continent getContinentbyId(int id){
        Query q = em.createNamedQuery("Continent.getById");
        q.setParameter(1, id);
        Continent continent = (Continent)q.getSingleResult();
        return continent;
    }
    @Override
    public Continent updateContinent(Continent continent)
    {
        tx.begin();

        Continent c = em.find(Continent.class, continent.getId());
        c.setContinentNaam(continent.getContinentNaam());
        
        em.persist(c);

        tx.commit();
        return c;
    }
    @Override
    public boolean deleteContinent(int id)
    {
        try {
            tx.begin();

            Continent c = em.find(Continent.class, id);
            em.remove(c);
            tx.commit();
            return true;
        } catch (Exception exc) {
            return false;
        }
    }
}
